#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

pkgnames=(xochitl)
pkgdesc="Read documents and take notes"
url=https://remarkable.com
pkgver=0.0.0-21
timestamp=2022-11-07T20:19:57Z
section="readers"
maintainer="Mattéo Delabre <spam@delab.re>"
license=MIT
# TODO: toltec-base is now installed by default for new users. It is listed
# below as a transitional dependency to ensure that it gets installed for
# existing users (see toltec-dev/toltec#532 for context). This dependency
# should be removed once the package has reached enough users
installdepends=(toltec-base)

source=(
    xochitl
    xochitl.png
    xochitl.draft
    xochitl.oxide
    toltec-after-launcher.conf
    toltec-wrapper.conf
    env-readme
    manual-sync@.service
    launcherctl-xochitl
)
sha256sums=(
    SKIP
    SKIP
    SKIP
    SKIP
    SKIP
    SKIP
    SKIP
    SKIP
    SKIP
)

package() {
    install -d "$pkgdir"/opt/etc/draft \
        "$pkgdir"/opt/etc/draft/icons \
        "$pkgdir"/opt/etc/xochitl.env.d \
        "$pkgdir"/etc/systemd/system/xochitl.service.d \
        "$pkgdir"/etc/systemd/system/remarkable-shutdown.service.d \
        "$pkgdir"/etc/systemd/system/remarkable-reboot.service.d

    install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/xochitl
    install -D -m 644 "$srcdir"/env-readme "$pkgdir"/opt/etc/xochitl.env.d/README

    install -D -m 644 -t "$pkgdir"/opt/etc/draft "$srcdir"/xochitl.draft
    install -D -m 644 -t "$pkgdir"/opt/etc/draft/icons "$srcdir"/xochitl.png
    install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/xochitl.oxide
    install -D -T -m 755 "$srcdir"/launcherctl-xochitl "$pkgdir"/opt/share/launcherctl/"$pkgname"

    install -D -m 644 -t "$pkgdir"/etc/systemd/system/xochitl.service.d \
        "$srcdir"/toltec-wrapper.conf
    install -D -m 644 -t "$pkgdir"/etc/systemd/system/remarkable-shutdown.service.d \
        "$srcdir"/toltec-after-launcher.conf
    install -D -m 644 -t "$pkgdir"/etc/systemd/system/remarkable-reboot.service.d \
        "$srcdir"/toltec-after-launcher.conf
    install -D -m 644 -t "$pkgdir"/etc/systemd/system \
        "$srcdir"/manual-sync@.service
}

configure() {
    systemctl daemon-reload

    # sync.service and rm-sync.service interfere with launchers
    # we use manual-sync@.service instead
    if [ -f /usr/bin/sync ] && ! is-masked sync.service; then
        systemctl mask sync.service
    fi

    if [ -f /usr/bin/rm-sync ] && ! is-masked rm-sync.service; then
        systemctl mask rm-sync.service
    fi

    # manual-sync.service has been changed to manual-sync@.service
    # Make sure to disable and stop the old version
    disable-unit manual-sync.service

    if [ -f /usr/bin/rm-sync ] && ! is-active manual-sync@rm-sync.service; then
        systemctl enable --now manual-sync@rm-sync.service
    elif [ -f /usr/bin/sync ] && ! is-active manual-sync@sync.service; then
        systemctl enable --now manual-sync@sync.service
    fi

    if is-enabled xochitl.service && ! is-enabled launcher.service; then
        # This package changes xochitl.service to alias it to launcher.service
        # when enabled. If xochitl was previously enabled, force the creation
        # of this alias by re-enabling the service
        systemctl enable xochitl.service 2> /dev/null
    fi
}

preremove() {
    disable-unit manual-sync@rm-sync.service
    disable-unit manual-sync@sync.service
}

postremove() {
    systemctl daemon-reload

    if [ -f /usr/bin/sync ] && is-masked sync.service; then
        systemctl unmask sync.service
    fi
    if [ -f /usr/bin/rm-sync ] && is-masked rm-sync.service; then
        systemctl unmask rm-sync.service
    fi

    if is-enabled xochitl.service && is-enabled launcher.service; then
        # If xochitl is currently the active launcher, make sure the
        # launcher.service alias is removed
        systemctl disable xochitl.service 2> /dev/null
        systemctl enable xochitl.service 2> /dev/null
    fi
}
